package org.mentalsmash.tazio.transactions.management;

import java.util.concurrent.Future;

import org.mentalsmash.tazio.transactions.TransactionStatus;
import org.mentalsmash.tazio.transactions.identifiers.TID;

/**
 * This class is a concrete implementation of the {@link TazioTransactionImpl}
 * class, implementing the singleton pattern.
 * <p>
 * The instance provided by this class is not a real transactions, but it is
 * only used as a placeholder for null transactions where the value
 * <code>null</code> cannot be used
 * </p>
 * <p>
 * Quite every operation of this class throws an
 * {@link UnsupportedOperationException}
 * </p>
 * 
 * @version 0.0.1 24/ott/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
public class NullTazioTransaction extends TazioTransactionImpl {

    private static final long serialVersionUID = 6155814276255910244L;
    private static NullTazioTransaction instance = null;

    private NullTazioTransaction() {
	super(null, null, TID.NULL_TID);
	this.setStatus(TransactionStatus.NO_TRANSACTION);
	this.setSeqNo(0);
    }

    public static NullTazioTransaction getInstance() {
	if (instance == null) {
	    instance = new NullTazioTransaction();
	}

	return instance;
    }

    /**
     * <strong>Operation not supported</strong>
     * 
     * @throws UnsupportedOperationException
     */
    @Override
    public void commit() {
	throw new UnsupportedOperationException();
    }

    /**
     * <strong>Operation not supported</strong>
     * 
     * @throws UnsupportedOperationException
     */
    @Override
    public Future<TransactionStatus> commitAsync() {
	throw new UnsupportedOperationException();
    }

    /**
     * <strong>Operation not supported</strong>
     * 
     * @throws UnsupportedOperationException
     */
    @Override
    public void rollback() {
	throw new UnsupportedOperationException();
    }

    /**
     * <strong>Operation not supported</strong>
     * 
     * @throws UnsupportedOperationException
     */
    @Override
    public void setRollbackOnly() {
	throw new UnsupportedOperationException();
    }

    /**
     * NullTransactions are equals if they have the same NULL TID. Actually no
     * transaction other then NullTazioTransaction should have a NULL_TID
     */
    @Override
    public boolean equals(final Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	final NullTazioTransaction other = (NullTazioTransaction) obj;
	if (this.getTransactionId() == null) {
	    if (other.getTransactionId() != null) {
		return false;
	    }
	} else if (!this.getTransactionId().equals(other.getTransactionId())) {
	    return false;
	}
	return true;
    }

    @Override
    public int hashCode() {
	final int prime = 3121;
	final int result = 1;
	return prime
		* result
		+ ((this.getTransactionId() == null) ? 0 : this
			.getTransactionId().hashCode());
    }

    @Override
    public String toString() {
	return "NULL_TRANSACTION";
    }

}
